/**
 * 服务版本管理初始化
 */
var AppVersion = {
    id: "AppVersionTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
AppVersion.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '主键id', field: 'id', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: '服务id', field: 'appId', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: '版本号', field: 'versionCode', align: 'center', valign: 'middle', sortable: true},
        {title: '版本功能', field: 'versionFunc', align: 'center', valign: 'middle', sortable: true},
        {title: '版本描述', field: 'versionDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '是否已上传', field: 'versionPath', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value==''){
                return "<span class='label label-danger'>未上传</span>";
        	}else{
                return "<span class='label label-success'>已上传</span>";
        	}
        }},
        {title: '管理', field: 'id', width:'300px', align: 'center', valign: 'middle', sortable: false,formatter:function(value,row,index){
        	var _html = "";
        	_html += "<button type='button' class='btn btn-info btn-xs btn-margin' onclick='AppVersion.publish_dev(\""+row.id+"\")'><i class='fa fa-upload' aria-hidden='true'></i>  发布到开发环境</button>"
        	if(hasProPublishPermission){
        		_html += "<button type='button' class='btn btn-warning btn-xs btn-margin' onclick='AppVersion.publish_pro(\""+row.id+"\")'><i class='fa fa-upload' aria-hidden='true'></i>  发布到生产环境</button>"
        	}
        	_html += "<button type='button' class='btn btn-success btn-xs btn-margin' onclick='AppVersion.download(\""+row.versionPath+"\")'><i class='fa fa-download' aria-hidden='true'></i>  下载</button>"
        	return _html;
        }},
    ];
};

/**
 * 发布版本到开发环境
 */
AppVersion.publish_dev = function(id){
    var operation = function(){
	    var ajax = new $ax(Sys.backendPath + "/appversion/publish/dev", function (data) {
	        Sys.success("发布成功!");
	        AppVersion.table.refresh();
	    }, function (data) {
	        Sys.error("发布失败!" + data.responseJSON.message + "!");
	    });
	    ajax.set("versionId",id);
	    ajax.start();
    };
    Sys.confirm("是否发布版本管理?",operation);
}

/**
 * 发布版本到生产环境
 */
AppVersion.publish_pro = function(id){
    var operation = function(){
	    var ajax = new $ax(Sys.backendPath + "/appversion/publish/pro", function (data) {
	        Sys.success("发布成功!");
	        AppVersion.table.refresh();
	    }, function (data) {
	        Sys.error("发布失败!" + data.responseJSON.message + "!");
	    });
	    ajax.set("versionId",id);
	    ajax.start();
    };
    Sys.confirm("是否发布版本管理?",operation);
}
/**
 * 下载版本
 */
AppVersion.download = function (path) {
	window.location.href=Sys.ctxPath+"/file/download/"+path;
}
/**
 * 检查是否选中
 */
AppVersion.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        AppVersion.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加服务版本
 */
AppVersion.openAddAppVersion = function () {
    var index = layer.open({
        type: 2,
        title: '添加服务版本',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/appversion/to_add/'+ appInstanceId
    });
    this.layerIndex = index;
};

/**
 * 打开查看服务版本详情
 */
AppVersion.openAppVersionDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '服务版本详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Sys.backendPath + '/appversion/to_update/' + AppVersion.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除服务版本
 */
AppVersion.del = function () {
    if (this.check()) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/appversion/delete", function (data) {
	            Sys.success("删除成功!");
	            AppVersion.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("appVersionId",AppVersion.seItem.id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除服务版本?",operation);
    }
};

/**
 * 查询服务版本列表
 */
AppVersion.search = function () {
    var queryData = {};
    queryData['appId'] = appInstanceId;
    queryData['versionCode'] = $("#versionCode").val();
    queryData['versionFunc'] = $("#versionFunc").val();
    queryData['versionDesc'] = $("#versionDesc").val();
    AppVersion.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = AppVersion.initColumn();
    var table = new BSTable(AppVersion.id, Sys.backendPath + "/appversion/list", defaultColunms);
    table.setPaginationType("server");
    var queryData = {'appId':appInstanceId};
    table.setQueryParams(queryData);
    table.setSortName("versionCode");
    AppVersion.table = table.init();
});
